using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace FrameWork
{
    public class UIGroup
    {
        private LinkedList<UIInstance> _groupDialogs;

        public UIGroup()
        {
            _groupDialogs = new LinkedList<UIInstance>();
        }

        public void AddUIDialog(UIInstance uiInstance)
        {
            var node = _groupDialogs.AddFirst(uiInstance);
            var nextUI = node.Next;
            if (nextUI != null)
            {
                var uiLogic = nextUI.Value.DialogLogic;
                uiLogic.OnCoverd();
            }
        }

        public void RemoveDialog(int id)
        {
            var firstNode = _groupDialogs.First;
            LinkedListNode<UIInstance> curNode = firstNode;
            while (curNode != null)
            {
                if (curNode.Value.DialogId == id)
                {
                    LinkedListNode<UIInstance> nextNode = curNode.Next;
                    _groupDialogs.Remove(curNode);
                    if (nextNode != null)
                    {
                        //界面被取消覆盖
                        nextNode.Value.DialogLogic.OnUnCoverd();
                    }
                    return;
                }

                curNode = curNode.Next;
            }
        }

        public void RemoveDialog(Type dialogType)
        {
            var firstNode = _groupDialogs.First;
            LinkedListNode<UIInstance> curNode = firstNode;
            while (curNode != null)
            {
                if (curNode.Value.GetDialog().GetType() == dialogType)
                {
                    LinkedListNode<UIInstance> nextNode = curNode.Next;
                    _groupDialogs.Remove(curNode);
                    if (nextNode != null)
                    {
                        //界面被取消覆盖
                        nextNode.Value.DialogLogic.OnUnCoverd();
                    }
                    return;
                }

                curNode = curNode.Next;
            }
        }

        /// <summary>
        /// 返回界面实例
        /// </summary>
        public UIInstance GetUIInstance(Type dialogType)
        {
            var firstNode = _groupDialogs.First;
            LinkedListNode<UIInstance> curNode = firstNode;
            while (curNode != null)
            {
                var curInstance = curNode.Value.GetDialog();
                if (curInstance.GetType() == dialogType)
                {
                    return curNode.Value;
                }
            }

            return null;
        }

        public UIInstance GetUIInstance(int uiId)
        {
            var firstNode = _groupDialogs.First;
            LinkedListNode<UIInstance> curNode = firstNode;
            while(curNode != null)
            {
                int id = curNode.Value.DialogId;
                if(id == uiId)
                    return curNode.Value;
            }
            return null;
        }

        public void Update(float deltaTime)
        {
            foreach(UIInstance uiInstance in _groupDialogs)
            {
                uiInstance.Update(deltaTime);
            }
        }
    }
}

